<template>
  <div class="container" v-title data-title="每日领取优惠券">
    <div class="activities">
      <div class="thumbnail" >
        <img :src="require('../styles/asset/coupon/coupon_01.png')">
      </div>
      <div class="thumbnail" >
        <img :src="require('../styles/asset/coupon/coupon_02.png')">
      </div>
      <div class="submit-btn">
        <a @click="getCoupon()"><img :src="require('../styles/asset/coupon/coupon_04.png')"></a>
      </div>
      <div class="thumbnail" >
        <img :src="require('../styles/asset/coupon/coupon_07.png')">
      </div>
      <div class="thumbnail" >
        <img :src="require('../styles/asset/coupon/coupon_08.png')">
      </div>
      <div class="thumbnail" >
        <img :src="require('../styles/asset/coupon/coupon_09.png')">
      </div>
    </div>
    <simplert ref="simplert"></simplert>
  </div>
</template>

<script>
  import Simplert from '../../components/dialog/dialog.vue';
  import wx from 'weixin-js-sdk'

  var filters = {
    parseURL:function(url) {
      var a = document.createElement('a');
      a.href = url;
      return {
        params: (function () {
          var ret = {},
            param = a.href.split('?')[1],
            seg = param.split('&'),
            len = seg.length,
            i = 0,
            s;
          for (; i < len; i++) {
            if (!seg[i]) {
              continue;
            }
            s = seg[i].split('=');
            ret[s[0]] = s[1];
          }
          return ret;
        })()
      }
    }
  }

  export default {
    data () {
      return {
        time:"",
        coupon:"",
        token:"",
        id:"",
        role:"",
        state:false,
        roles:false,
        btn: '立即领取',
        img: require('../styles/asset/share.jpg'),
        wx_app:''
      }
    },
    created () {

    },
    mounted(){
      const that = this;
      const url = window.location.href;
      this.id = filters.parseURL(url).params.id;
      if(url.indexOf('wx_app')>0){
        this.wx_app = filters.parseURL(url).params.wx_app;
        this.detail = filters.parseURL(url).params.detail;
      }else{
        this.wx_app = ''
      }
      this.$util.getUserToken().then(token=>{
        that.token = token;
      })
      let root = process.env.MAIN_ROOT
      console.log(root+this.img)
      this.$util.sendShareMessage("每日领取优惠券","每天免费骑行一小时，优惠券持续发放中", root+this.img);

      if(url.indexOf('wx_app')>0){
        let newUrl = this.$util.wxUrl(url);
        // 小程序分享title，url
        wx.miniProgram.postMessage({
          data: {
            title: '每日领取优惠券',
            wx_url: newUrl
          }
        })
      }
    },
    components: {
      Simplert
    },
    methods: {
      open (title, message, type) {
        let obj = {
          title: title,
          message: message,
          type: type
        }
        this.$refs.simplert.openSimplert(obj)
      },
      getCoupon () {
        let that = this;
        let userAgentObj = navigator.userAgent;
        if(!this.wx_app){
            if(userAgentObj.match(/MicroMessenger/i)){
              //$(".weiChat").show();
              if(userAgentObj.match(/Android/i)){
                window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919'
              }else if(userAgentObj.match(/iPhone/i)) {
                window.location.href = "xingjiakejiIOS:///#/activity/getcouponDaily"
                window.setTimeout(function(){
                  window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8"
                },2000)
              }else{
                window.location.href="http://download.xingjiakeji.com/"
              }
              return;
            }else{
              if(!this.token || this.token == undefined || this.token == ""){
                if(userAgentObj.match(/Android/i)){
                  window.location.href = "xingjiakejiIOS:///#/activity/getcouponDaily"
                }
                window.setTimeout(function(){
                  if(userAgentObj.match(/Android/i)){
                    window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919'
                  }else if(userAgentObj.match(/iPhone/i)){
                    window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8"
                  }
                },2000);
                return;
              }
            }
        }

        this.$api.post('/coupon/v1/authApi/getActivityCoupon?id='+this.id,{},s=>{
          this.open('优惠券发放成功','','fade_success');
          this.state = true;
        },f=>{
          this.open(f.errorMessage,'','fade_alert');
        },{
          'requestPlatform': that.wx_app,
          'token': that.token
        })

      },
    }
  }
</script>

<style scoped >
  @import "../styles/styles.css";
  .thumbnail{ padding: 0; margin: 0;}
  .thumbnail>img{ width: 100%;}
  .container{ position: relative; width: 100%; }
  .activities{ overflow-y: scroll;  }
  ul.myul{ padding-bottom: 1rem;}
  ul.myul>li{  padding: 1rem; position: relative; padding-left: 3rem; color:#666;}
  ul.myul>li:after{ content: ""; position: absolute; display:block; left: 1.5rem; top: 1.4rem;  width: 0.5rem; height: 0.5rem; border: 2px solid rgba(245,205,125,1); border-radius: 50%;}
  .submit-btn{ padding:0; text-align: center; background: url('../styles/asset/coupon/coupon_06.png') no-repeat center; background-size: 100% 100%; width: 100%;}
  .submit-btn>a{ width: 61%; display: inline-block;  margin:0 auto; }
  .submit-btn>a>img{ width: 100%;}
</style>
